from django.contrib import admin

from .models import History, Notice, Advice, Progress


@admin.register(History)
class HistoryAdmin(admin.ModelAdmin):
    list_display = ('__str__', 'content_display')

    @admin.display(description='内容')
    def content_display(self, obj):
        content = obj.content
        return content[:50] + '……' if len(content) > 50 else content


@admin.register(Notice)
class NoticeAdmin(admin.ModelAdmin):
    list_display = ('title', 'content_display', 'time_display')

    @admin.display(description='内容')
    def content_display(self, obj):
        content = obj.content
        return content[:50] + '……' if len(content) > 50 else content

    @admin.display(description='时间')
    def time_display(self, obj):
        time = obj.time
        return time.strftime('%Y年%m月%d日 %H时%M分%S秒')


@admin.register(Advice)
class AdviceAdmin(admin.ModelAdmin):
    readonly_fields = ('title', 'content', 'sender')
    list_display = ('__str__', 'content_display', 'finished')
    list_editable = ('finished', )

    @admin.display(description='内容')
    def content_display(self, obj):
        content = obj.content
        return content[:50] + '……' if len(content) > 50 else content

    def has_add_permission(self, request):
        return False


@admin.register(Progress)
class ProgressAdmin(admin.ModelAdmin):
    list_display = ('name', 'value', 'start_date')
    list_editable = ('value', 'start_date')
